<?php
/**
 * 轮播模型
 */

namespace app\common\model;

use think\model\concern\SoftDelete;
use think\db;
class Slide extends Model
{
    use SoftDelete;
    const mobile = 1;
    public $softDelete = true;
    protected $name = 'slide';
    protected $autoWriteTimestamp = true;
    protected $defaultPage = 1;
    protected $defaultLimit = 5;
    //可搜索字段
    protected $searchField = ['name',];

    public $BannerList = [
        ['bm' => 'index_db_banner', 'name' => '首页顶部banner','cc'=>'750*750'],
        ['bm' => 'index_zb_banner', 'name' => '首页中部banner','cc'=>'690*280'],
    ];


    /**
     * 广告管理列表
     * @param $get
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function lists($get)
    {
        $ad = new Slide();
        $where = [];
        $where[] = ['delete_time', '=', 0];
        if (isset($get['keyword']) && $get['keyword']) {
            $where[] = ['name', 'like', '%' . $get['keyword'] . '%'];
        }

        if (isset($get['pid']) && $get['pid'] != '') {
            $where[] = ['pid', '=', $get['pid']];
        }

        $ad_count = $ad
            ->where($where)
            ->count();
        $adPage= $get['page'] ?? $ad->defaultPage;
        $adLimit = $get['limit'] ?? $ad->defaultLimit;
        $ad_list = $ad->where($where)
            ->page($adPage, $adLimit)
            ->order('id desc')
            ->select();

        $position = Db::name('ad_position')->where('delete_time', 0)->column('id,name', 'id');

        foreach ($ad_list as $item) {
            $item['image'] = $item['image'];
            $url = '';
            if (isset($position[$item['pid']])) {
                $item['position_name'] = $position[$item['pid']];
            }
            switch ($item['link_type']) {
                case 1:
                    $page = Slide::getLinkPage(1, $item['link']);
                    $url = '商城页面：' . $page['name'];
                    break;
                case 2:
                    $goods = Db::name('goods g')
                        ->where(['g.id' => $item['link']])
                        ->field('g.name,min_price,max_price')
                        ->find();
                    if ($goods) {
                        $price = '￥' . $goods['max_price'];
                        if ($goods['max_price'] !== $goods['min_price']) {
                            $price = '￥' . $goods['min_price'] . '~' . $goods['max_price'];
                        }
                        $url = '商品页面:' . $goods['name'] . '价格：' . $price;
                    }
                    break;
                case 3:
                    $url = '自定义链接：' . $item['link'];
            }
            $item['link'] = $url;


        }
        return ['count' => $ad_count, 'list' => $ad_list];
    }


    public static function getLinkPage($type=1,$from = true){
        $page = [
            self::mobile    => [
                [
                    'name'      => '商品分类',
                    'path'      => '/pages/sort/sort',
                    'is_tab'    => 1,
                ],
                // [
                //     'name'      => '领券中心',
                //     'path'      => '/pages/user_getcoupon/user_getcoupon',
                //     'is_tab'    => 0,
                // ],
                [
                    'name'      => '个人中心',
                    'path'      => '/pages/user/user',
                    'is_tab'    => 1,
                ],
                // [
                //     'name'      => '积分签到',
                //     'path'      => '/bundle/pages/user_sign/user_sign',
                //     'is_tab'    => 0,
                // ],
                [
                    'name'      => '会员中心',
                    'path'      => '/pages/user_vip/user_vip',
                    'is_tab'    => 0,
                ],
                [
                    'name'      => '积分抽奖',
                    'path'      => '/bundle/pages/luckly_wheel/luckly_wheel',
                    'is_tab'    => 0,
                ],
                [
                    'name'      => '限时秒杀',
                    'path'      => '/bundle/pages/goods_seckill/goods_seckill',
                    'is_tab'    => 0,
                ],
                [
                    'name'      => '拼团活动',
                    'path'      => '/bundle/pages/goods_combination/goods_combination',
                    'is_tab'    => 0,
                ],
                [
                    'name'      => '砍价活动',
                    'path'      => '/bundle/pages/bargain/bargain',
                    'is_tab'    => 0,
                ],
                [
                    'name'      => '热销榜单',
                    'path'      => '/bundle/pages/hot_list/hot_list',
                    'is_tab'    => 0,
                ],
                [
                    'name'      => '商城资讯',
                    'path'      => '/pages/news_list/news_list',
                    'is_tab'    => 0,
                ],
                [
                    'name'      => '帮助中心',
                    'path'      => '/pages/news_list/news_list?type=1',
                    'is_tab'    => 0,
                ],
            ],
        ];
        $page = $page[$type] ?? [];
        if(true === $from){
            return $page;
        }
        return $page[$from] ?? [];
    }

    public static function getGoodsPath($from = true){
        $desc = [
            self::mobile    => '/pages/goods_details/goods_details',
        ];
        if(true === $from){
            return $desc;
        }
        return $desc[$from] ?? '';

    }

    /**
     * Notes:筛选首页推荐分类
     * @return array|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @author:  2021/3/6 12:01
     */
    public static function getGoodsCategory(){
        return Db::name('goods_category')->where(['delete_time' => 0])->field('id,name')->select();
    }


    /**
     * 获取广告位置列表
     * @return array
     */
    public static function infoPosition()
    {
        $position_list = Db::name('ad_position')
            ->where(['status' => 1, 'delete_time' => 0])
            ->group('name')
            ->column('id,name', 'id');
        asort($position_list);
        return $position_list;
    }


    /**
     * 添加
     * @param $post
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function add($post)
    {
        $Slide = new Slide();

        $post['type'] = isset($post['type']) ? $post['type'] : 1;
        $post['status'] = isset($post['status']) ? $post['status'] : 0;
        $post['link_type'] = isset($post['link_type']) ? $post['link_type'] : '';
        $link = '';

        switch ($post['link_type']) {
            case '1':
                $link = $post['page'];
                break;
            case '2':
                $link = $post['goods_id'];
                break;
            case '3':
                $link = $post['url'];
                break;
        }
        $time = time();
        $data = [
            'name'          => $post['name'],
            'pid'           => $post['pid'],
            'image'         => $post['image'],
            'link_type'     => $post['link_type'],
            'link'          => $link,
            'status'        => $post['status'],
            'category_id'   => $post['category_id'] ?? 0,
            'create_time'   => $time,
        ];
        return $Slide->allowField(true)->save($data);
    }


    /**
     * 编辑
     * @param $post
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function edit($post)
    {
        $ad = new Slide();
        $post['type'] = isset($post['type']) ? $post['type'] : 1;
        $post['status'] = isset($post['status']) ? $post['status'] : 0;
        $post['link_type'] = isset($post['link_type']) ? $post['link_type'] : '';
        $time = time();
        $link = '';
        switch ($post['link_type']) {
            case '1':
                $link = $post['page'];
                break;
            case '2':
                $link = $post['goods_id'];
                break;
            case '3':
                $link = $post['url'];
                break;
        }
        $data = [
            'name'          => $post['name'],
            'client'        => $post['client'],
            'pid'           => $post['pid'],
            'image'         => $post['image'],
            'link_type'     => $post['link_type'],
            'link'          => $link,
            'category_id'   => $post['category_id'] ?? 0,
            'status'        => $post['status'],
            'update_time'   => $time,

        ];
        return $ad->allowField(true)->save($data, ['id' => $post['id']]);
    }


    /**
     * 删除
     * @param $delData
     * @return int|string
     * @throws Exception
     * @throws \think\exception\PDOException
     */
    public static function del($id, $client)
    {
        $data = [
            'delete_time' =>time(),
            'update_time' => time(),
        ];
        return Db::name('slide')->where(['delete_time' => 0, 'id' => $id])->update($data);

    }

    /**
     * 广告表信息
     * @param $id
     * @return mixed
     */
    public static function info($id)
    {
        $ad = new Slide();
        $info = $ad::get(['id' => $id]);
        $info['goods'] = '';
        if ($info['link_type'] == 2) {
            $goods = Db::name('goods g')
                ->where(['g.id' => $info['link']])
                ->field('g.id, g.name, g.image, min_price, max_price')
                ->find();
            $price = '￥' . $goods['max_price'];
            if ($goods['max_price'] !== $goods['min_price']) {
                $price = '￥' . $goods['min_price'] . '~' . $goods['max_price'];
            }
            $goods['price'] = $price;
            $goods['image'] =$goods['image'];
            $info['goods'] = $goods;
        }
        $info['abs_image'] =$info['image'];

        $info['position_desc'] = '';
        if (isset($info['pid'])) {
            $position = Db::name('ad_position')
                ->field('width, height')
                ->where(['id' => $info['pid']])
                ->find();
            $info['position_desc'] = '建议上传广告图片宽*高, '.$position['width'].'px*'.$position['height'].'px';
        }

        return $info;
    }

    /**
     * 更改状态
     * @param $get
     * @return int|string
     * @throws Exception
     * @throws \think\exception\PDOException
     */
    public static function switchStatus($get)
    {
        $data = [
            'status' => $get['status'],
            'update_time' => time(),
        ];
        return Db::name('slide')->where(['delete_time' => 0, 'id' => $get['id']])->update($data);
    }
}

